using System.Collections.Generic;
using UnityEngine;

public class CustomisationLayer : MonoBehaviour
{
	public GameObject mainBackgroundCamera;

	public new UILabel name;

	public GameObject customisationCamera;

	public List<CustomisationTabController> customisationTabs = new List<CustomisationTabController>();

	public SkinCustomisationController skinController;

	public GenderCustomisationButton maleButton;

	public GenderCustomisationButton femaleButton;

	public UILabel nameLabel;

	private Persona myPersona;

	private Dictionary<Persona.Gender, Persona> genderSpecificLooks = new Dictionary<Persona.Gender, Persona>();

	private void OnEnable()
	{
		mainBackgroundCamera.SetActive(false);
		customisationCamera.SetActive(true);
		UITools.ChangeText(name, GGPlayerSettings.instance.Model.name);
		myPersona = new Persona(GGPlayerSettings.instance);
		UpdateCustomisationView();
	}

	private void UpdateCustomisationView()
	{
		foreach (CustomisationTabController customisationTab in customisationTabs)
		{
			customisationTab.UpdateWithPersona(myPersona);
		}
		skinController.UpdateWithPersona(myPersona);
		maleButton.DisplayEnabledStatusUpdate(myPersona);
		femaleButton.DisplayEnabledStatusUpdate(myPersona);
	}

	private void OnDisable()
	{
		SetActiveCameraIfNotNull(customisationCamera, false);
		SetActiveCameraIfNotNull(mainBackgroundCamera, true);
	}

	private void SetActiveCameraIfNotNull(GameObject camera, bool activeStatus)
	{
		if (camera != null)
		{
			camera.SetActive(activeStatus);
		}
	}

	public void OnGenderSelect(Persona.Gender newGender)
	{
		SelectGender(newGender);
		UpdateCustomisationView();
	}

	private void SaveGenderSpecificLooks()
	{
		genderSpecificLooks[myPersona.gender] = myPersona;
	}

	private void SelectGender(Persona.Gender newGender)
	{
		Persona persona = new Persona(newGender);
		Persona persona2 = null;
		if (genderSpecificLooks.ContainsKey(newGender))
		{
			persona2 = genderSpecificLooks[newGender];
		}
		foreach (Persona.LookSelection item in myPersona.myConfig)
		{
			if (item.selectedLook != null && item.selectedLook.IsAvailable(persona))
			{
				continue;
			}
			if (item.selectedLook != null)
			{
				CustomisationConfig.Look similarGenderLook = GetSimilarGenderLook(item.selectedLook.lookName, newGender, item.type);
				if (similarGenderLook != null)
				{
					persona.ChangeLook(similarGenderLook);
					continue;
				}
			}
			if (persona2 == null)
			{
				continue;
			}
			foreach (Persona.LookSelection item2 in persona2.myConfig)
			{
				if (item.type == item2.type && item2.selectedLook != null && item2.selectedLook.IsAvailable(persona))
				{
					persona.ChangeLook(item2.selectedLook);
					break;
				}
			}
		}
		myPersona = persona;
	}

	private CustomisationConfig.Look GetSimilarGenderLook(string name, Persona.Gender gender, CustomisationConfig.LookSetType type)
	{
		if (type == CustomisationConfig.LookSetType.Hair)
		{
			return null;
		}
		CustomisationConfig.LookSet lookSetForType = CustomisationConfig.instance.GetLookSetForType(type);
		return lookSetForType.GetLookByName(name, gender);
	}

	public void SaveSelection()
	{
		GGPlayerSettings.instance.SetName(nameLabel.text);
		myPersona.SavePreferences();
	}
}
